<?php

class model_books extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }

    public function getBook($id) {
        $query = $this->db->query("SELECT * FROM document WHERE id_document = '$id' ");
        $book = $query->row_array();
        return $book;
    }

    public function getBookInfo($id) {
        $sql = "SELECT * FROM document, location WHERE id_document = '$id' AND document.location = location.id_location";
        $query = $this->db->query($sql);
        $book = $query->row_array();
        return $book;
    }
    
    public function searchBook($text) {
    	$query = $this->db->query("SELECT * FROM document, categories WHERE document.category = categories.id_cat AND name_document LIKE '%$text%'");
        $list = [];
        foreach ($query->result() as $row) {
        	array_push($list, $row);
        }
        return $list;
    }

    public function getList() {
        $sql = "SELECT * FROM document, categories WHERE document.category = categories.id_cat";
        $query = $this->db->query($sql);
        
        return $query->result_array();
    }

    public function getBookbyUser($username) {
        $sql = "SELECT * FROM document, categories WHERE document.category = categories.id_cat
            AND document.username = '$username'";
        $query = $this->db->query($sql);

        return $query->result_array();
    }
/**
 * ============================SHOW BOOK INFO=============================================
*/
    public function show_info($id){
         $sql = "SELECT * FROM document, location WHERE id_document = '$id' AND document.location = location.id_location";
         $query = $this->db->query($sql);
         $result = $query->row_array();
         return $result;
    }

/**
 * ============================UPDATE BOOK INFO=============================================
*/
    public function update_info($id_doc, $title, $author, $descript, $cate, $tags, $loca){
        $data = array(
            'name_document' => $title,
            'author' => $author,
            'description' => $descript,
            'category' => $cate,
            'tags' => $tags,
            'location' => $loca
            );
        $this->db->where('id_document', $id_doc);
        $this->db->update('document', $data);
    }
/**
 * ============================DELETE BOOK INFO=============================================
*/
    public function delete_book($id_doc){
        $this->db->where('id_document', $id_doc);
        $this->db->delete('document');
    }

//===============================UPDATE READING=============================================
    public function update_reading($user, $book, $chapter) {
        $this->db->where('user', $user);
        $this->db->where('id_document', $book);
        $query = $this->db->get('reading');

        if ($query->num_rows > 0) {
            $data = array('id_chap' => $chapter);
            $this->db->where('user', $user);
            $this->db->where('id_document', $book);
            $this->db->update('reading', $data);
        }
        else 
        {
            $data = array(
                'user' => $user,
                'id_document' => $book,
                'id_chap' => $chapter
            );
            $this->db->insert('reading', $data);
        }
    }
}
?>